package com.zyt.controller;

import com.zyt.anno.Log;
import com.zyt.pojo.Dept;
import com.zyt.pojo.Result;
import com.zyt.service.DeptService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
   // private static final Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;

    //@RequestMapping(value="/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list() {
//        System.out.println("查询全部的部门数据");
        log.info("查询全部的部门数据");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }
    //删除部门
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request) {
//        String id = request.getParameter("id");
//        int i = Integer.parseInt(id);
//        System.out.println("根据ID删除部门:"+i);
//        return Result.success();
//    }
    //方式二: @RequestParam

    //    @DeleteMapping("/depts")
//    public Result delete(@RequestParam("id") Integer deptId) {
//        System.out.println("根据ID删除部门:"+deptId);
//        return Result.success();
//    }
    //方式三: 省略@RequestParam(前端传递的请求参数名与服务端方法形参名一致)
    @DeleteMapping
    @Log
    public Result delete(Integer id) {
//        System.out.println("根据ID删除部门:" + id);
        log.info("根据ID删除部门:{}",id);
        deptService.delete(id);
        return Result.success();
    }

    /*
    添加部门
     */
    @PostMapping
    @Log
    public Result save(@RequestBody Dept dept) {
//        System.out.println("添加部门:" + dept);
        log.info("添加部门:{}",dept);
        deptService.add(dept);
        return Result.success();
    }

    //    /*
//    根据id查询部门
//     */
//    @GetMapping("/depts/{id}")
//    public Result getInfo(@PathVariable("id") Integer deptId){
//        System.out.println("根据id查询部门:"+deptId);
//        return Result.success();
//    }
     /*
    根据id查询部门
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
//        System.out.println("根据id查询部门:" + id);
        log.info("根据id查询部门:{}",id);
        Dept dept = deptService.getByid(id);
        return Result.success(dept);
    }

    /*
    修改部门
     */
    @PutMapping
    @Log
    public Result update(@RequestBody Dept dept) {
//        System.out.println("修改部门:" + dept);
        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }

}
